#!/data/Software/mydan/perl/bin/perl -I/data/Software/mydan/JOB/lib -I/data/Software/mydan/JOB/private/lib
use strict;
use warnings;
use utf8;
use Code;
use Util;
use Logs;
use LWP::UserAgent;
use JSON;
use POSIX;
use Encode;

$| ++;

=head1 SYNOPSIS

    db => $mysql,

=cut

return sub
{
    my %param = @_;
    my ( $db, $logs ) = ( $param{db}, Logs->new( 'server.approval' ) );

    while(1)
    {
        warn "do ...\n";

        my $x = eval{ $db->query( "select id,user,cont,submitter,oauuid from approval where opinion='unconfirmed' and oauuid != '1'" ) };
        $logs->die( "mysql query fail: $@" ) if $@;
        $logs->die( "get approval info from mysql fail" ) unless defined $x && ref $x eq 'ARRAY';

        for ( @$x )
        {
            my ( $id, $user, $cont, $submitter, $oauuid ) = @$_;
            utf8::encode($cont); 
            utf8::encode($user); 
            utf8::encode($submitter); 

            my %env = Util::envinfo( qw( appname appkey ) );
            my $ua = LWP::UserAgent->new;
            $ua->default_header( %env );
 
            if( $oauuid )
            {
                my $res = $ua->get( "http://api.connector.open-c3.org/connectorx/approval?uuid=$oauuid" );

                if( $res->is_success )
                {
                    my $data = eval{JSON::from_json $res->content};
                    if( $data && $data->{stat})
                    {
                        if( $data->{data}{status} eq 'agree' || $data->{data}{status} eq 'refuse' )
                        {
                            my $time = POSIX::strftime( "%Y-%m-%d %H:%M:%S", localtime );
                            eval{ $db->execute( "update approval set opinion='$data->{data}{status}',finishtime='$time' where id=$id and opinion='unconfirmed'" ) };
                            $logs->err( "update approval.opinion status fail:$@" ) if $@;
                        }
                    }#TODO
                }
            }
            else
            {
                eval{ $db->execute( "update approval set oauuid='1' where id=$id and oauuid='0'" ) };
                $logs->err( "update approval.oauuid status fail:$@" ) if $@;

                my %form = (
                    content => $cont,
                    submitter => $submitter,
                    approver => $user,
                );
                my $res = $ua->post( "http://api.connector.open-c3.org/connectorx/approval", 
                    Content => JSON::to_json(\%form), 'Content-Type' => 'application/json'
                );

                if( $res->is_success )
                {
                    my $data = eval{JSON::from_json $res->content};
                    if( $data && $data->{stat} && $data->{stat} =~ /^[a-zA-Z0-9\._\-]+$/ )
                    {
                        eval{ $db->execute( "update approval set oauuid='$data->{data}' where id=$id and oauuid='1'" ) };
                        $logs->err( "update approval.oauuid status fail:$@" ) if $@;
                    }#TODO
                }

            }
        }

        sleep 45;
    }
}
